package com.isoftstone.common.utils.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOReadAndWrite {

    private static final String lineSeparator = "\r\n";

    private File fileText = new File("E:\\11.txt");

    public void readTest(File fileName) {
        String content = null;
        FileInputStream fis = null;
        FileChannel fc = null;
        try {
            fis = new FileInputStream(fileName);
            fc = fis.getChannel();
            ByteBuffer bb = ByteBuffer.allocate(10000);
            fc.read(bb);
            bb.flip();
            content = new String(bb.array());
            System.out.println(content);
            fc.close();
            fis.close();
        } catch (IOException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //NIO写数据
    public void writeTest(File file, String content) {
        FileOutputStream fos = null;
        FileChannel fc = null;
        byte[] b = (content + lineSeparator).getBytes();
        try {
            fos = new FileOutputStream(file, true);
            fc = fos.getChannel();
            ByteBuffer bb = ByteBuffer.allocate(b.length);
            bb.put(b);
            bb.flip();
            fc.write(bb);
            fc.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void copyFile(String oldPath, String newPath) throws IOException {
        long starttime = System.currentTimeMillis();
        ByteBuffer bb = ByteBuffer.allocate(100000);
        File oldf = new File(oldPath);
        File newf = new File(newPath);
        if (!newf.exists()) {
            newf.mkdirs();
        }
        int len = -1;
        File temp = null;
        String[] list = oldf.list();
        for (int i = 0; i < list.length; i++) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + list[i]);
            } else {
                temp = new File(oldPath + File.separator + list[i]);
            }
            System.out.println(temp.isFile());
            if (temp.isFile()) {
                System.out.println(temp.getPath());
                FileInputStream fis = new FileInputStream(temp);
                FileChannel in = fis.getChannel();

                FileOutputStream fos = new FileOutputStream(newf + "/" + temp.getName().toString().replaceAll("a", "xxxx"));
                FileChannel out = fos.getChannel();
                while ((len = in.read(bb)) != -1) {
                    bb.flip();
                    out.write(bb);
                    bb.clear();
                }

                new NIOReadAndWrite().writeTest(fileText, temp.getPath() + "*******改:*******" + temp.getName().toString().replaceAll("a", "xxxx"));
                out.close();
                fos.close();
                in.close();
                fis.close();
            }
            if (temp.isDirectory()) {
                copyFile(oldPath + "/" + list[i], newPath + "/" + list[i]);
            }
        }


        long endtime = System.currentTimeMillis();
        System.out.println("复制总时长：" + (endtime - starttime));

    }

    public static void main(String[] args) throws IOException {
        String oldFile = "F:\\testfile";
        String newFile = "F:\\testfile1";
        new NIOReadAndWrite().copyFile(oldFile, newFile);
    }
}